/**
 *	@project:	kybernetes
 *	@author:	D.Toepel
 *	@note:		---
 *	
 *	Copyright (C) 2009  D.Toepel <de.dtools> {at} <gmail> {dot} <com>
 *	
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *	
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 */
package dt.htwk.rmi;

import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RMIClassLoader;
import java.security.Permission;

import dt.htwk.core.KCStatusBar;

import sun.security.provider.PolicyFile;

/**
 * <h3>RMIClient</h3>
 *
 * @author DToepel
 *
 */
public class RMIClient{
	
	/**
	 * serialVersionUID -- Standard: 1L
	 */
	private static final long serialVersionUID = 1L;
	
	private static String serverAddress = "localhost";
	private static String serverName = RMIConnector.REGISTRY_NAME;
	private static Integer serverPort = RMIConnector.RMI_PORT;
	private static RMIConnector con = null;
	
	public static void setServerAddress(String addr){
		RMIClient.serverAddress = addr;
	}

	/**
	 * <h3>Connect2Server</h3>
	 *
	 * @return
	 */
	public synchronized static Boolean Connect2Server()//
	{
		Boolean isConnect = false;
		Boolean regLookup = true;
		Boolean namLookup = true;
		while(regLookup){
			try
			{
				System.setProperty("java.rmi.server.hostname",serverAddress);
				String answer;
				
				if(namLookup){
		        // 1. Versuch mit Naming.lookup
					System.out.println("//" + serverAddress + ":" + serverPort + "/" + serverName);//
					con = (RMIConnector)Naming.lookup(
							"//" + serverAddress + ":" + serverPort + "/" + serverName);//
					answer = con.setTestText("client with Naming.lookup");
					regLookup = false;
				}else{
		        // 2. Versuch mit registry.lookup
					System.out.println("try registry.lookup");
			        Registry registry = 
		                LocateRegistry.getRegistry(serverAddress, serverPort);
			        for (String string : registry.list()) {
			        	System.out.println("registry.list : " + string);
					}
		            con = (RMIConnector) registry.lookup(serverName);
		            answer = con.setTestText("client with registry.lookup");
		            regLookup = false;
				}
				KCStatusBar.setStatusMessage(answer);
				System.out.println(answer);
	            isConnect = true;
			}
			catch (Exception e){
				if(!namLookup && regLookup) regLookup = false;
				if(namLookup) namLookup = false;
				isConnect = false;
				e.printStackTrace();
				
			}
		}
		return isConnect;
	}// END Connect2Server 

}
